$(document).ready( function(){

	//add to cart
	$('a.add_cart').click( function(){
		var product = $(this).attr('data-product');
		var url = window.site_url + 'checkout/cart/add';
		var qty_input = $("#quantity_"+product);
		var option = $('select[name=product_option_id]').val();
		//console.log('url:',url);
	
		var qty = 1;
		if(qty_input.length == 1){
			qty = qty_input.val();

		}

		$.ajax({
		  url: url+"/"+product+"/"+qty+"/"+option,
		  dataType: 'json'
		}).done(function(data) {
			
			if(data.status == 'success'){
				$("html, body").animate({ scrollTop: 0 }, 600);
				//console.log('message success:', data);
			}
		  	else{
		  		$("html, body").animate({ scrollTop: 0 }, 600);
		  		//console.log('message error:', data);
		  	}

		});
	});

	//update cart
	$('input.update_cart').click( function(){
		var id = $(this).attr('cart-id');
		var product_value = $('#'+id).val();
		var product_id = $(this).attr('data-product');
		var url = window.site_url + 'checkout/cart/update';

		//console.log('id:',id);
		//console.log('product_value:',product_value);

		$.ajax({
		  url: url+"/"+id+"/"+product_value+"/"+product_id,
		  dataType: 'json'
		}).done(function(data) {
			
			if(data.status == 'success'){				
				//console.log('message success:', data);
				window.location.reload();
			}		  	
		  	else{
		  		$("html, body").animate({ scrollTop: 0 }, 600);
		  		//console.log('message error:', data);
		  	}

		});
	});

	//checkout step 
	$('a.step-checkout').click( function(){
		var curentStep = $(this).attr('data-curent');
		var nextStep = $(this).attr('data-continue');

		if(curentStep == 'checkout_options'){
			var option = $('input[name=step_option]:checked').val();
			var url = window.site_url + 'checkout/validation_step1';
			$.post(url, { "data_option": option },
			function(data){
				if(data == 0){
					alert('Account Information is required!');
					return false;
				}else{
					$('#step1').hide();
					$('#step2').hide();
					$('#step3').hide();
					$('#step4').hide();
					$('#step5').hide();

					if(nextStep == 'personal_details'){
						
						//if user select user account address 

						if (option == 'using_account'){
							var url = window.site_url + 'checkout/set_user_personal_detail';

							$.ajax({
							  type: "POST",
							  url: url,
							  dataType: 'json',
							}).done(function(data) {								

								$('input[name=first_name]').val(data.first_name);
								$('input[name=last_name]').val(data.last_name);
								$('input[name=email]').val(data.email);

								$('input[name=address_1]').val(data.address_1);
								$('input[name=address_2]').val(data.address_2);
								$('input[name=telephone]').val(data.telephone);
								$('input[name=city]').val(data.city);
								$('input[name=post_code]').val(data.post_code);
								$('select[name=country]').val(data.country);
								$('select[name=region]').val(data.region);

							});


							$('.password_form').hide();
						}

						else if (option == 'using_new'){
							$('input[name=first_name]').val('');
							$('input[name=last_name]').val('');
							$('input[name=email]').val('');

							$('input[name=address_1]').val('');
							$('input[name=address_2]').val('');
							$('input[name=telephone]').val('');
							$('input[name=city]').val('');
							$('input[name=post_code]').val('');
							$('select[name=country]').val('');
							$('select[name=region]').val('');

							$('.password_form').hide();
						}

						//console.log(option);

						$('#step2').fadeIn();
					}						
					else{						
						alert('Please follow the steps.');
						$('#step1').fadeIn();

						var button = $('#checkout_options');

						button.attr('data-curent','checkout_options');
						button.attr('data-continue','personal_details');
					}						
				}
			});
		
		}
		else if(curentStep == 'personal_details'){
			var first_name 	= $('input[name=first_name]');
			var last_name 	= $('input[name=last_name]');
			var email 		= $('input[name=email]');
			//var password 	= $('input[name=password]');
			//var c_password 	= $('input[name=c_password]');

			var password = $('#password_register');
			var c_password = $('#c_password_register');


			var address1 	= $('input[name=address_1]');
			var address2 	= $('input[name=address_2]');
			var telephone  	= $('input[name=telephone]');
			var city  		= $('input[name=city]');
			var post_code 	= $('input[name=post_code]');
			var country  	= $('select[name=country]');
			var region 		= $('select[name=region]');

			var option = $('input[name=step_option]:checked').val();

			if($.trim(first_name.val()) == ''){
				alert('First Name is required!');
				first_name.focus();
				return false;
			}
			else if($.trim(last_name.val()) == ''){
				alert('Last Name is required!');
				last_name.focus();
				return false;
			}
			else if($.trim(email.val()) == ''){
				alert('Email is required!');
				email.focus();
				return false;
			}
			else if(!check_email($.trim(email.val()))) {
				alert('Email is not valid!');
				email.focus();
				return false;
			}
			else if($.trim(password.val()) == '' && option == 'register'){
				alert('Password is required!');
				password.focus();
				return false;
			}
			else if($.trim(c_password.val()) == '' && option == 'register'){
				alert('Confirm Password is required!');
				c_password.focus();
				return false;
			}
			else if($.trim(password.val()) != $.trim(c_password.val()) && option =='register'){
				alert('Confirm Password does not match with password!');
				c_password.focus();
				return false;
			}
			else if($.trim(address1.val()) == ''){
				alert('Address is required!');
				address1.focus();
				return false;
			}
			else if($.trim(address2.val()) == ''){
				alert('Address is required!');
				address2.focus();
				return false;
			}
			else if($.trim(telephone.val()) == ''){
				alert('Telephone is required!');
				telephone.focus();
				return false;
			}
			else if($.trim(city.val()) == ''){
				alert('City is required!');
				city.focus();
				return false;
			}
			else if($.trim(post_code.val()) == ''){
				alert('Post Code is required!');
				post_code.focus();
				return false;
			}
			else if($.trim(country.val()) == ''){
				alert('Country is required!');
				country.focus();
				return false;
			}
			else if($.trim(region.val()) == ''){
				alert('Region or State is required!');
				region.focus();
				return false;
			}else{

				var url = window.site_url + 'checkout/validation_step2';
				$('#error_step2').html('');

				$.ajax({
				  type: "POST",
				  url: url,
				  data:{'first_name':first_name.val(),'last_name':last_name.val(),'email':email.val(),'password':password.val(),'c_password':c_password.val(),'address1':address1.val(),'address2':address2.val(),'telephone':telephone.val(),'city':city.val(),'post_code':post_code.val(),'country':country.val(),'region':region.val(), 'option':option},
				  dataType: 'json',
				}).done(function(data) {
					
					if(data.status == 'error'){
						$.each(data.error, function(index, value) {
  							$('.'+index).html(value);
						});
					}		  	
				  	else{
				  		$('#step1').hide();
						$('#step2').hide();
						$('#step3').hide();
						$('#step4').hide();
						$('#step5').hide();

						if(nextStep == 'shipping_method'){

							var url = window.site_url + 'checkout/shipping/get_shipping_method';

							$.ajax({
								 type: "POST",
								 url: url,
							}).done(function(data) {
								$('#content_step3').html(data);
							});

							$('#step3').fadeIn();
						}						
						else{		
							alert('Please follow the steps.');
							$('#step1').fadeIn();

							var button = $('#checkout_options');

							button.attr('data-curent','checkout_options');
							button.attr('data-continue','personal_details');
						}
				  	}

				});
				
			}

		}
		
		else if (curentStep == 'shipping_method'){
			val = $('#shipping_form').serialize();
			url = window.site_url + 'checkout/shipping/set_shipping_method';
			$.ajax({
				type: "POST",
				url: url,
				data:{'delivery_method':val},
				dataType: 'json',
			}).done(function(data) {

				if(data.status == 'error'){
					$.each(data.error, function(index, value) {
						$('.'+index).html(value);
					});
				}		  	
				else{
					$('#step1').hide();
					$('#step2').hide();
					$('#step3').hide();
					$('#step4').hide();
					$('#step5').hide();

					if(nextStep == 'payment_method_form'){
						$('#step4').fadeIn();
					}						
					else{
						alert('Please follow the steps.');
						$('#step1').fadeIn();

						var button = $('#checkout_options');

						button.attr('data-curent','checkout_options');
						button.attr('data-continue','personal_details');
					}
				}

			});
		}
		else if(curentStep == 'payment_method_form'){
			var payment_method = $('input[name=payment_method]:checked').val();

			if($.trim(payment_method) == '') {
				alert('Payment Method is required!');
				return false;
			}
			else{
				var url = window.site_url + 'checkout/validation_step4';

				$.ajax({
				  type: "POST",
				  url: url,
				  data:{'payment_method':payment_method},
				  dataType: 'json',
				}).done(function(data) {
					
					if(data.status == 'error'){
						$.each(data.error, function(index, value) {
  							$('.'+index).html(value);
						});
					}		  	
				  	else{
				  		$('#step1').hide();
						$('#step2').hide();
						$('#step3').hide();
						$('#step4').hide();
						$('#step5').hide();

						if(nextStep == 'payment_confirmation'){

							url_confirmation = window.site_url + 'checkout/get_confirmation_cart';
							$.ajax({
							  type: "POST",
							  url: url_confirmation,
							  data:{'confirmation':1},
							}).done(function(data) {
								$('#content_step5').html(data);
								
							});

							$('#step5').fadeIn();
							
						}						
						else{
							alert('Please follow the steps.');
							$('#step1').fadeIn();

							var button = $('#checkout_options');

							button.attr('data-curent','checkout_options');
							button.attr('data-continue','personal_details');
						}
					}

				});
			}
		}
		else if(curentStep == 'confirmation'){
			if(nextStep == 'finish'){
				$('#checkout_form').submit();
			}else{
				alert('Please follow the steps.');
				$('#step1').fadeIn();	

				var button = $('#payment_method_form');

				button.attr('data-curent','payment_method_form');
				button.attr('data-continue','payment_confirmation');
			}

			//console.log(curentStep);
			//console.log(nextStep);
		}
		else{
			alert('Please follow the steps.');
			$('#step1').fadeIn();

			var button = $('#checkout_options');

			button.attr('data-curent','checkout_options');
			button.attr('data-continue','personal_details');
		}

	});

	$("#country").change(function() {
		var countryId = $(this).val();
		//console.log(countryId);
		var url = window.site_url + 'checkout/get_region/'+countryId;
		$('#region').empty();

		$.post(url, function(data) {
			if(data == ''){
				
				$('#region').append('<option> None </option>');
			}
			else{
				
  				$('#region').append(data);
  			}
		});		
	});
	
	$("#product_option").change(function(){
		var optionId = $(this).val();
		var stok = $('a.add_cart').attr('data-product');
		if(optionId == ''){
			var url = window.site_url + 'checkout/get_stok/'+stok;
			$('#stok').empty();
			
			$.post(url, function(data){
				if(data <= '0'){
					$('#stok').append('Persediaan Habis');
				}else{
					$('#stok').append(data);
				}
				
			});
		}else{
			var url = window.site_url + 'checkout/get_option_stok/'+optionId;
			
			$('#stok').empty();
			
			$.post(url, function(data){
				if(data <= '0'){
					$('#stok').append('Persediaan Habis');
				}else{
					$('#stok').append(data);
				}
				
			});
		}
	});

	$(".btn_back").click(function() {
		var curentStep = $(this).attr('data-curent');

		$('#step1').hide();
		$('#step2').hide();
		$('#step3').hide();
		$('#step4').hide();
		$('#step5').hide();

		if (curentStep == 'personal_details'){
			$('#step1').fadeIn();
		}
		else if (curentStep == 'shipping_method'){
			$('#step2').fadeIn();
		}
		else if (curentStep == 'payment_method_form'){
			$('#step3').fadeIn();
		}
		else if (curentStep == 'confirmation'){
			$('#step4').fadeIn();
		}

	});
	
});

(function( $ ) {
  $.fn.rightClick = function(method) {
    $(this).bind('contextmenu rightclick', function(e){
        //e.preventDefault();
        method();
        //return false;
    })

  };
})( jQuery );


$(document).rightClick(function(e){
		var no = 1;
        $(document).bind('mousemove',function(e){ 
        	if(no == 1){
        		//console.log( e.pageX + " - " + e.pageY); 
        		no++;		
        	}
		});
});

